package com.sicau.socialpracticesystem.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import com.sicau.socialpracticesystem.service.SqlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.sql.Statement;

@Service
@Slf4j
public class SqlServiceImpl implements SqlService {

    @Resource
    private DruidDataSource druidDataSource;

    @Override
    public void executeInsert(String insertSqlStatement) {
        try (
                DruidPooledConnection connection = druidDataSource.getConnection();
                Statement statement = connection.createStatement();
        ) {
            statement.execute(insertSqlStatement);
        } catch (SQLException e) {
            log.error("批量插入数据库时发生异常");
            throw new RuntimeException("批量插入数据库时发生异常");
        }
    }
}
